//
//  BookShopingRootViewController.m
//  QiYReader
//
//  Created by 奇阅科技 on 14/12/12.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "BookShopingRootViewController.h"
#import "PathHelper.h"
#import "BookShoppingWebViewController.h"
#import "BookrackCollectionViewController.h"
#import "AppDelegate.h"
#import "ZMYNetWorkAPI.h"
#import "BookSearchViewController.h"
#import "ZMYSettingCollectionViewController.h"

@interface BookShopingRootViewController()
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
@end
@implementation BookShopingRootViewController
-(void)viewDidLoad{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    
    self.pageControl.backgroundColor = [UIColor clearColor];
    self.pageControl.textColor = [UIColor colorWithRed:171/255.0 green:206/255.0 blue:141/255.0 alpha:1];
    self.pageControl.selectionIndicatorColor = [UIColor whiteColor];
    self.pageControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.pageControl.selectionIndicatorHeight = 3;
    self.pageControl.selectedTextColor = [UIColor whiteColor];
    self.pageControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    self.pageControl.showVerticalDivider = NO;
    NSArray *titleArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[[PathHelper documentDirectoryPathWithName:@"db"]stringByAppendingPathComponent:@"bookShop"]];
    if (titleArray.count>1) {
        [self creatChildView:titleArray];
    }else{
        NSString *currentAppVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
        NSDictionary *dict1 = @{@"title": @"精选",
                                @"url": [NSString stringWithFormat:@"http://%@/web/store.php?a=index&tpl_style=style_2014&pt=client&ct=iOS&srcid=iOS&v=%@",SERVER,currentAppVersion]};
        NSDictionary *dict2 = @{@"title": @"免费",
                                @"url": [NSString stringWithFormat:@"http://%@/web/store.php?a=free&tpl_style=style_2014&pt=client&ct=iOS&srcid=iOS&v=%@",SERVER,currentAppVersion]};
        NSDictionary *dict3 = @{@"title": @"排行",
                                @"url": [NSString stringWithFormat:@"http://%@/web/store.php?a=ranking&tpl_style=style_2014&pt=client&ct=iOS&srcid=iOS&v=%@",SERVER,currentAppVersion]};
        NSDictionary *dict4 = @{@"title": @"书评",
                                @"url": [NSString stringWithFormat:@"http://%@/web/store.php?a=shuping&tpl_style=style_2014&pt=client&ct=iOS&srcid=iOS&v=%@",SERVER,currentAppVersion]};
        NSDictionary *dict5 = @{@"title": @"分类",
                                @"url": [NSString stringWithFormat:@"http://%@/web/store.php?a=cat&tpl_style=style_2014&pt=client&ct=iOS&srcid=iOS&v=%@",SERVER,currentAppVersion]};
        titleArray = @[dict1,dict2,dict3,dict4,dict5];
        [self creatChildView:titleArray];
        [NSKeyedArchiver archiveRootObject:titleArray toFile:[[PathHelper documentDirectoryPathWithName:@"db"]stringByAppendingPathComponent:@"bookShop"]];
    }
    [[ZMYNetWorkAPI default]getBookShopTitle];
    
    
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(backToBookRackTap)];
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(backToBookRackTap)];
    
    self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    
    [self.pageViewController.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
    [self.pageViewController.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
-(void)creatChildView:(NSArray *)titleArray{
    self.pages = [[NSMutableArray alloc]init];
    for (NSDictionary *dict in titleArray) {
        BookShoppingWebViewController * childView = [self.storyboard instantiateViewControllerWithIdentifier:@"BookShoppingWebViewController"];
        childView.titleStr = [dict objectForKey:@"title"];
        childView.urlStr = [dict objectForKey:@"url"];
        [self.pages addObject:childView];
    }
}
- (BookrackCollectionViewController *)frostedBookrackViewController
{
    for (UIViewController *iter in self.navigationController.viewControllers) {
        if ([iter isKindOfClass:[BookrackCollectionViewController class]]) {
            return (BookrackCollectionViewController *)iter;
        }
    }
    return nil;
}
- (ZMYRootViewController *)frostedViewController
{
    UIViewController *iter = self.parentViewController;
    while (iter) {
        if ([iter isKindOfClass:[ZMYRootViewController class]]) {
            return (ZMYRootViewController *)iter;
        } else if (iter.parentViewController && iter.parentViewController != iter) {
            iter = iter.parentViewController;
        } else {
            iter = nil;
        }
    }
    return nil;
}
- (void)backToBookRackTap{
    [self backToBookRack:nil];
}
- (IBAction)backToBookRack:(id)sender {
    if ([self frostedBookrackViewController]) {
        [self.navigationController popToViewController:[self frostedBookrackViewController] animated:YES];
    }else if ([self frostedViewController]){
        [[self frostedViewController] hideSideViewController:YES];
    }else{
        [[AppDelegate delegate] goToBookRack];
    }
    
}
- (IBAction)toSearchView:(id)sender {
    BookSearchViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"BookSearchViewController"];
    [self.navigationController pushViewController:view animated:YES];
}
- (IBAction)myInfoView:(id)sender {
    if ([self frostedSettingCollectionViewController]) {
        [self.navigationController popToViewController:[self frostedSettingCollectionViewController] animated:YES];
    }else{
        ZMYSettingCollectionViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"ZMYSettingCollectionViewController"];
        [self.navigationController pushViewController:view animated:YES];
    }
}
- (ZMYSettingCollectionViewController *)frostedSettingCollectionViewController
{
    for (UIViewController *iter in self.navigationController.viewControllers) {
        if ([iter isKindOfClass:[ZMYSettingCollectionViewController class]]) {
            return (ZMYSettingCollectionViewController *)iter;
        }
    }
    return nil;
}
@end
